<?php
namespace common\behaviors;

use yii\base\Behavior;
use common\helpers\Constants;
use common\helpers\TreeHelper;

class TreeBehavior extends Behavior
{

    private $_level;

    public function getLevel()
    {
        return $this->_level;
    }

    public function setLevel($value)
    {
        $this->_level = $value;
    }

    public function getLevelName()
    {
        return str_repeat(Constants::TAB_SIZE, $this->level) . $this->owner->name;
    }

    private $_parentIds;

    public function getParentIds()
    {
        if ($this->_parentIds === null) {
            $this->_parentIds = TreeHelper::getParentIds($this->owner->className(), $this->owner->parent_id);
        }
        return $this->_parentIds;
    }

    private $_childrenIds;

    public function getChildrenIds()
    {
        if ($this->_childrenIds === null) {
            $this->_childrenIds = TreeHelper::getChildrenIds($this->owner->className(), $this->owner->id);
        }
        return $this->_childrenIds;
    }
}
